package com.situ.mall.controller.back;

import java.io.Console;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.mall.entity.Category;
import com.situ.mall.entity.CategoryCount;
import com.situ.mall.entity.Order;
import com.situ.mall.entity.OrderItem;
import com.situ.mall.entity.Product;
import com.situ.mall.entity.Result;
import com.situ.mall.entity.Shipping;
import com.situ.mall.service.ICategoryService;
import com.situ.mall.service.IOrderService;
import com.situ.mall.service.IProductService;
import com.situ.mall.service.IShippingService;

@Controller
@RequestMapping("/orderShow")
public class OrderShowController {
	@Autowired
	private ICategoryService categoryService;
	@Autowired
	private IProductService productService;
	@Autowired
	private IOrderService orderService;
	@Autowired
	private IShippingService shippingService;
	
	@RequestMapping("/getOrderShowPage.action")
	public String getOrderShowPage() {
		
		return "order_list";
	}

	@RequestMapping("/getOrderItemPage.action")
	public String getOrderItemPage(Long orderNo,Model model) {
		System.out.println("OrderShowController.getOrderItemPage()");
		System.out.println("orderNo = " + orderNo);
		//查到该No的Order
		Order order = orderService.selectByOrderNo(orderNo);
		//查到该No下的Item
		List<OrderItem> orderItemList = orderService.selectOrderItemByOrderId(orderNo);
		for (OrderItem orderItem : orderItemList) {
			System.out.println(orderItem);
		}
		order.setOrderItemList(orderItemList);
		System.out.println(order);
		//查到收货地址信息
		Shipping shipping = shippingService.selectByShippingId(order.getShippingId());
		System.out.println("shipping =" + shipping);
		
		model.addAttribute("shipping", shipping);
		model.addAttribute("order", order);
		return "order_orderItem";
	}

	@RequestMapping("/selectByPage.action")
	@ResponseBody
	public Result selectByPage(@RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "5")Integer limit) {
		System.out.println("OrderShowController.selectByPage()");
		Result result = orderService.selectByPage(page, limit);
		return result;
	}

	@RequestMapping("/deleteAll.action")
	@ResponseBody
	public Result deleteAll(/* String ids */ Integer[] ids,Long[] orderNos) {
		// System.out.println("ids: " + ids);
		// String[] array = ids.split(",");
		System.out.println(Arrays.toString(ids));// [26, 27]
		System.out.println(Arrays.toString(orderNos));// [26, 27]
		System.out.println("ids = " +ids.length);
		System.out.println("orders = " +orderNos.length);
		boolean isSuccess = orderService.deleteAll(ids,orderNos);
		System.out.println( " is = " + isSuccess);
		if (isSuccess) {
			return Result.success("删除成功");
		} else {
			return Result.error("删除失败");
		}
	}
	
	
	@RequestMapping("/update.action")
	@ResponseBody
	public Result update(Category category) {
		System.out.println(category.getId());
		boolean isSuccess = categoryService.update(category);
		if (isSuccess) {
			return Result.success("修改成功");
		} else {
			return Result.error("修改失败");
		}
	}

	@RequestMapping("/deleteById")
	@ResponseBody
	public Result deleteById(Integer id,Long orderNo) {
		System.out.println("OrderShowController.deleteById()");
		System.out.println("id = " + id);
		System.out.println("orderNo = " + orderNo);
		
		boolean isSuccess = orderService.deleteById(id,orderNo);
		System.out.println(isSuccess);
		if (isSuccess) {
			return Result.success("删除订单成功");
		}else {
			return Result.error("删除失败");
		}
	}
}
